<?php

	/* Note: This thumbnail creation script requires the GD PHP Extension.  
		If GD is not installed correctly PHP does not render this page correctly
		and SWFUpload will get "stuck" never calling uploadSuccess or uploadError
	 */
  include('../init.php');
  include ("../includes/class.upload.php");
  
  $project_id = $_POST["project_id"];
//
//  $image = new image();
//  
//
//	ini_set("html_errors", "0");
//
//	// Check the upload
//	if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
//		echo "ERROR:invalid upload";
//		exit(0);
//	}
//
//	$name = get_file_name($project_id, $_FILES['Filedata']['tmp_name']);
//  $path = "../../images/projects/$project_id";
//  
//  if(!file_exists($path)){
//	mkdir($path);
//  }
//  $urlpath = get_url_path($path);
//  $link = URL."/".$urlpath."/".$name;
//  $filename = $path ."/". $name;
//
//  if (!move_uploaded_file($_FILES['Filedata']['tmp_name'], $filename)){
//    echo 'Problem: Could not move file to destination directory';
//    exit(0);
//  }
//
//  ini_set('memory_limit', '100M');
//  $image->load($filename);
//  $image->maxWidth(350);
//  $image->maxHeight(260);
//  $image->save($filename);
//  ini_set('memory_limit', '16M');
//
//  chmod($filename,0755);
//
//	if (!isset($_SESSION["file_info"])) {
//		$_SESSION["file_info"] = array();
//	}
//
//	// Use a output buffering to load the image into a variable
//	ob_start();
//  $image->output();
//  $image->destroy();
//	
//	$imagevariable = ob_get_contents();
//	ob_end_clean();
//
//  $sql = "INSERT INTO project_img(project_id,project_img_path,project_img_show) VALUES('$project_id','$name','1')";
//  $db->execute($sql);
//
//  $file_id = md5($filename + rand()*100000);
//
//  $_SESSION["file_info"][$file_id] = $imagevariable;

//echo "FILEID:" . $file_id; // Return the file id to the script

	if(isset($_FILES["Filedata"]) && is_uploaded_file($_FILES["Filedata"]["tmp_name"]) && $_FILES["Filedata"]["tmp_name"]!=''){
		$handle = new Upload($_FILES['Filedata']);
		if ($handle->uploaded)
		{
			$handle->image_resize = true;
			$handle->image_ratio_fill = true;
			$handle->image_x = 350;
			$handle->image_y = 260;
			$handle->Process('../../images/projects/'.$project_id);
		
			if ($handle->processed)
			{
				$sql = "INSERT INTO project_img(project_id,project_img_path,project_img_show) VALUES('$project_id','".$handle->file_dst_name."','1')";
				if(!mysql_query($sql))
				{
					@unlink('../../images/projects/'.$project_id.'/'.$handle->file_dst_name);
				}
				else
				{
					//Use a output buffering to load the image into a variable
					ob_start();
					//header('Content-type: ' . $handle->file_src_mime);
					//echo $handle->Process();
					//die();
					echo '<img src="../../images/projects/'.$project_id.'/'.$handle->file_dst_name.'" />';
					$imagevariable = ob_get_contents();
					ob_end_clean();
	
					$file_id = md5("../../images/projects/$project_id/".$handle->file_dst_name + rand()*100000);
	
					$_SESSION["file_info"][$file_id] = $imagevariable;
					echo "FILEID:" . $file_id; // Return the file id to the script
				}
			}
			$handle->clean();
		}
	}
?>













